package com.ruoyi.web.api;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.otc.domain.*;
import com.ruoyi.otc.service.*;
import com.ruoyi.web.api.base.ApiBaseController;
import com.ruoyi.web.api.vo.*;
import com.ruoyi.web.constant.ApiConstant;
import com.ruoyi.web.util.SM2Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(value = "企业画像大屏接口",tags = "企业画像大屏接口")
@Slf4j
@RestController
@RequestMapping(ApiConstant.API_THREE)
public class ApiPortraitScreenController extends ApiBaseController {

    @Autowired
    private IBusinessService businessService;
    @Autowired
    private ILabelService labelService;
    @Autowired
    private ITopicService topicService;
    @Autowired
    private IBusinessTopicLogService businessTopicLogService;
    @Autowired
    private IBazaarRegulatorService bazaarRegulatorService;

    private static final Integer FIELD_COUNT = 198;

    @ApiOperation("企业画像大屏信息")
    @PostMapping("/portraitScreen")
    @ResponseBody
    public AjaxResult portraitScreen() {
        PortraitScreenVo respVo = new PortraitScreenVo();
        //模块画像统计
        List<ModuleTopic> moduleTopics = new ArrayList<>();
        List<Topic> topics = topicService.list(new QueryWrapper<Topic>().eq("del",false));
        for (Topic topic : topics) {
            int count = businessTopicLogService.count(new QueryWrapper<BusinessTopicLog>().eq("topic_name", topic.getName()));
            ModuleTopic moduleTopic = new ModuleTopic();
            moduleTopic.setTopicCount(count);
            moduleTopic.setTopicName(topic.getName());
            moduleTopics.add(moduleTopic);
        }
        respVo.setModuleTopics(moduleTopics);

        //企业画像标签统计
        List<LabelInfoVo> labelInfoVos = new ArrayList<>();
        List<Label> labels = labelService.list();
        for (Label label : labels) {
            LabelInfoVo labelInfoVo = new LabelInfoVo();
            labelInfoVo.setName(label.getName());
            labelInfoVo.setCount(label.getCount());
            labelInfoVos.add(labelInfoVo);
        }
        respVo.setLabelInfo(labelInfoVos);

        //其他数据量统计
        OtherInfoVo otherInfoVo = new OtherInfoVo();
        int businessCount = businessService.count();
        int governmentLabel = businessService.count(new QueryWrapper<Business>().last("where exist=1 or longin_capital=1 or possess=1 or rank1=1 or wheth=1 or whether=1"));
        int businessLabel = businessService.count(new QueryWrapper<Business>().last("where qysf = 1 or zjsf = 1"));
        otherInfoVo.setIndustryCommerce(businessCount);
        otherInfoVo.setGovernmentData(governmentLabel + businessCount*FIELD_COUNT);
        otherInfoVo.setBusinessData(businessLabel);
        respVo.setOtherInfoVo(otherInfoVo);
        //企业数字档案总数
        respVo.setPortraitCount(businessCount);
        //最高评分企业画像
        List<PortraitOrderVo> portraitOrderVos = new ArrayList<>();
        List<BusinessTopicLog> topicLogs = businessTopicLogService.list(new QueryWrapper<BusinessTopicLog>()
                .orderByDesc("sum_score").last("limit 10"));
        for (BusinessTopicLog topicLog : topicLogs) {
            PortraitOrderVo portraitOrderVo = new PortraitOrderVo();
//            if (ObjectUtils.isNotEmpty(topicLog.getBusinessId())){
//                List<Business> businesses = businessService.list(new QueryWrapper<Business>().eq("entname", topicLog.getBusinessName()));
//                if (businesses.size()>0 && ObjectUtils.isNotEmpty(businesses.get(0).getEnttypeCn())){
//                    portraitOrderVo.setEnttypeCn(businesses.get(0).getEnttypeCn());
//                }
//            }
            portraitOrderVo.setTopicName(topicLog.getTopicName());
            portraitOrderVo.setBusinessName(topicLog.getBusinessName());
            portraitOrderVo.setSumScore(topicLog.getSumScore());
            portraitOrderVo.setCreateTime(topicLog.getCreateTime());
            portraitOrderVos.add(portraitOrderVo);
        }
        respVo.setPortraitOrderVos(portraitOrderVos);


        //企业标签数量统计
        List<String> enttypeCnList = businessService.selectenttypeCn();
        List<BusinessDistribVo> businessDistribVoList = new ArrayList<>();
        for (String type : enttypeCnList) {
            int count = businessService.count(new QueryWrapper<Business>().eq("enttype_cn", type));
            BusinessDistribVo businessDistribVo = new BusinessDistribVo();
            businessDistribVo.setEnttypeCn(type);
            businessDistribVo.setCount(count);
            businessDistribVoList.add(businessDistribVo);
        }
        respVo.setBusinessDistribVo(businessDistribVoList);

        log.info("企业画像大屏信息返回数据：{}",respVo);
        return AjaxResult.success(respVo);
    }

    @ApiOperation("企业画像大屏企业信息")
    @PostMapping("/portraitBusinessInfo")
    @ResponseBody
    public AjaxResult portraitBusinessInfo() {
        PortraitBusinessInfoVo infoVo = new PortraitBusinessInfoVo();
        infoVo.setBusinessEvalCount(businessTopicLogService.count());
        List<BusinessInfo> businessInfos = new ArrayList<>();
        List<Business> list = businessService.list();
        for (Business business : list) {
            BusinessInfo businessInfo = new BusinessInfo();
            businessInfo.setEntname(business.getEntname());
            if (ObjectUtils.isEmpty(business.getUpdateTime())) {
                businessInfo.setDate(business.getCreateTime());
            } else {
                businessInfo.setDate(business.getUpdateTime());
            }
            List<BazaarRegulator> ls = bazaarRegulatorService.list(new QueryWrapper<BazaarRegulator>().eq("ent_name", business.getEntname()));
            if (ls.size()>0){
                businessInfo.setIndustryCo(ls.get(0).getIndustryCo());
            }
            businessInfos.add(businessInfo);
        }
        infoVo.setBusinessList(businessInfos);
        return AjaxResult.success(infoVo);
    }
}
